package com.yugu.park.dto;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

import com.yugu.park.exception.BizException;
import com.yugu.park.security.LoginHandlerInterceptor;

/**
 * @author guxf
 * @date 2021/10/27
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Authorization {
    private String id;
    private String shopId;//
    private String openId;//

    public static String authorization(String id, String secret) {
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", id);
        return Jwts.builder()
                .setClaims(claims)
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }
    public static String authorization(String id, String secret,String openId) {
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", id);
        claims.put("openId", openId);
        return Jwts.builder()
                .setClaims(claims)
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }
    public static Authorization parseAuthorization(String token, String secret) {
        Claims claims = Jwts.parser()
                .setSigningKey(secret)
                .parseClaimsJws(token)
                .getBody();
        return Authorization.builder()
                .id(claims.get("id", String.class))
                .shopId(claims.get("shopId", String.class))
                .openId(claims.get("openId", String.class))
                .build();
    }

    public static String getWxUserOpenId() {
        Authorization authorization = LoginHandlerInterceptor.AUTHORIZATION.get();
        if (authorization == null) {
            return null;
        }
        return authorization.getOpenId();
    }
    public static String getUid() {
        Authorization authorization = LoginHandlerInterceptor.AUTHORIZATION.get();
        if (authorization == null) {
            throw new BizException(401, "获取不到登录信息");
        }
        return authorization.getId();
    }
    public static String getClientId() {
        Authorization authorization = LoginHandlerInterceptor.AUTHORIZATION.get();
        if (authorization == null) {
            return null;
        }
        return authorization.getId();
    }
    public static String createToken(String uid, String secret) {
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", uid);
        return Jwts.builder()
                .setClaims(claims)
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }

    public static Authorization createAuthorization(String token, String secret) {
        Claims claims = Jwts.parser()
                .setSigningKey(secret)
                .parseClaimsJws(token)
                .getBody();
        return Authorization.builder()
                .id(claims.get("id", String.class))
                .build();
    }


}
